<template>
  <div class="department-container">
    <el-dialog :title="isEdit?'编辑':'添加'" :visible.sync="isShow" :close-on-click-modal="false" :close-on-press-escape="false" @close="hDialogClose">
      <deptDialog :id="curId" ref="deptDialog" :is-edit="isEdit" :origin-list="originList" @success="hSuccess" @close="hClose" />
    </el-dialog>
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        组织架构
        <el-row>
          <el-col
            :span="20"
          ><el-col
            class="grid-content bg-purple-light"
          >江苏传智播客教育科技股份有限公司</el-col></el-col>
          <el-col
            :span="4"
          ><div class="grid-content bg-purple">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span class="el-dropdown-link">
                    下拉菜单<i class="el-icon-arrow-down el-icon--right" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加1级部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </div></el-col>
        </el-row>
        <!-- tree -->
        <el-tree
          :data="data"
          :props="defaultProps"
          @node-click="handleNodeClick"
        >
          <template slot-scope="{ data }">
            <el-row style="width: 100%">
              <el-col
                :span="20"
              ><el-col class="grid-content bg-purple-light">{{
                data.name
              }}</el-col></el-col>
              <el-col
                :span="4"
              ><div class="grid-content bg-purple">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        下拉菜单<i
                          class="el-icon-arrow-down el-icon--right"
                        />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </div></el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments.js'
import { tranListToTreeData } from '@/utils/index'
import deptDialog from './deptDialog.vue'
export default {
  components: { deptDialog },
  data() {
    return {
      // 依赖一份树形数据
      data: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      isShow: false,
      curId: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    handleNodeClick(data) {
      // console.log(data)
    },
    async loadDepartments() {
      const res = await getDepartments()
      // console.log(res);
      res.data.depts.shift()
      // this.data=res.data.depts
      this.originList = res.data.depts.map(item => {
        return {
          id: item.id,
          code: item.code,
          name: item.name,
          pid: item.pid
        }
      }
      )
      this.data = tranListToTreeData(res.data.depts)
    },
    hAdd(id) {
      this.isShow = true
      this.curId = id
      this.isEdit = false
    },
    hSuccess() {
      // console.log('222')
      this.isShow = false
      this.loadDepartments()
    },
    hClose() {
      this.isShow = false
    },
    hDel(id) {
      this.$confirm('你确定要离开吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
      }).catch(() => {
        // 用户取消退出
      })
    },
    async doDel(id) {
      await delDepartment(id)
      this.loadDepartments()
      this.$message.success('删除成功')
    },
    async hEdit(id) {
      this.isShow = true
      this.isEdit = true
      this.curId = id
    },
    hDialogClose() {
      this.$refs.deptDialog.resetForm()
    }
  }
}
</script>

<style>
</style>
